from PySide6.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout
from PySide6.QtCore import Qt, Slot, QRect, QPropertyAnimation, QParallelAnimationGroup, QPoint

from searchEdit import searchLineEdit
from shortcutBar import shortcutsBar
from previewPage import previewWidget
from actionBar import actionsBar,windowIcon
from signalObject import widgetMove

def changePreviewAnimation(old_widget,new_widget,s=None):
    new_widget.show()
    old_x = old_widget.geometry().topLeft().x() + old_widget.width()

    old_move = QPropertyAnimation(old_widget, b'pos', parent=old_widget)
    old_move.setDuration(s if s else old_widget.width())
    old_move.setEndValue(QPoint(-old_widget.width(), 0))
    old_move.finished.connect(lambda :old_widget.hide())

    new_move = QPropertyAnimation(new_widget, b'pos', parent=new_widget)
    new_move.setDuration(s if s else old_x)
    new_move.setStartValue(QPoint(650,0))
    new_move.setEndValue(QPoint(0, 0))

    animationGroup = QParallelAnimationGroup(parent=old_widget)
    animationGroup.addAnimation(old_move)
    animationGroup.addAnimation(new_move)
    animationGroup.start()

    return animationGroup
def posAnimation(widget,x,y,s):
    move = QPropertyAnimation(widget, b'pos', parent=widget)
    move.setDuration(s)  # 设置动画时长，单位为毫秒
    move.setEndValue(QPoint(x, y))
    move.start()

def geometryAnimation(widget,left,top,width,height,s):
    move = QPropertyAnimation(widget, b'geometry', parent=widget)
    move.setDuration(s)  # 设置动画时长，单位为毫秒
    move.setEndValue(QRect(left, top, width, height))
    move.start()

class ContentWidget(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.initSetUp()

        self.moveSignal = widgetMove()

        self.mainPage = MainPage(parent=self)
        self.preview = None

        self.bind()

    def bind(self):
        self.moveSignal.topMove.connect(self.topScale)
        self.mainPage.urlEdit.parserSignal.connect(self.changePreview)

    def initSetUp(self):
        self.setObjectName('ContentWidget')
        self.setAttribute(Qt.WA_StyledBackground, True)  # 支持qss设置背景
        self.setFixedSize(650,350)

    @Slot(str)
    def changePreview(self,url:str):
        self.preview = previewWidget(url,parent=self)
        self.preview.closePreview.connect(lambda :changePreviewAnimation(self.preview,self.mainPage))
        changePreviewAnimation(self.mainPage,self.preview)

    @Slot(bool)
    def topScale(self,bl:bool=True):
        # https://pyqt5.com/QPropertyAnimation.html
        if bl:
            posAnimation(self, 0, -self.height(), self.height())
        else:
            posAnimation(self, 0, 0, self.height())


class MainPage(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent)

        self.layout = QVBoxLayout(self)
        self.urlEdit = searchLineEdit()
        self.linkBar = shortcutsBar()

        self.initSetUp()
        self.bind()

    def bind(self):
        pass

    def initSetUp(self):
        self.setObjectName('MainPage')
        self.setAttribute(Qt.WA_StyledBackground, True)  # 支持qss设置背景
        self.setFixedSize(self.parent().size())

        self.layout.addWidget(self.urlEdit, alignment=Qt.AlignmentFlag.AlignCenter)
        self.layout.addWidget(self.linkBar, alignment=Qt.AlignmentFlag.AlignHCenter)

        self.layout.setSpacing(15)
        self.layout.setContentsMargins(0, 0, 0, 40)

class MenuBar(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent)
        self._press = False

        self.moveSignal = widgetMove()
        self.bar = actionsBar()
        self.icon = windowIcon()

        self.initSetUp()
        self.bind()

    def bind(self):
        self.moveSignal.leftMove.connect(self.leftScale)

    def initSetUp(self):
        self.setFixedSize(650, 50)
        self.setAttribute(Qt.WA_StyledBackground, True)  # 支持qss设置背景
        self.setObjectName('topMenu')

        layout = QHBoxLayout(self)

        bar = QWidget()
        self.bar.setParent(bar)

        layout.addWidget(self.icon,alignment=Qt.AlignmentFlag.AlignVCenter)
        layout.addWidget(bar)

        layout.setSpacing(0)
        layout.setContentsMargins(0,0,0,0)

    @Slot(bool)
    def leftScale(self,bl:bool=True):
        if bl:
            geometryAnimation(self.bar,50,0,0,50,300)
        else:
            geometryAnimation(self.bar, 0, 0, self.bar.maximumWidth(), self.bar.maximumHeight(),300)

    def mousePressEvent(self, event):
        super().mousePressEvent(event)
        if event.button() == Qt.MouseButton.LeftButton:
            self._press = True
            self.mouseStartPos = event.globalPos()
            self.windowTopLeftPos = self.mapToGlobal(self.frameGeometry().topLeft()) # 获取屏幕坐标

    def mouseMoveEvent(self, event):
        super().mouseMoveEvent(event)
        if self._press:
            distance = event.globalPos() - self.mouseStartPos
            self.window().move(distance + self.windowTopLeftPos)

    def mouseReleaseEvent(self, event):
        super().mouseReleaseEvent(event)
        if self._press:
            self._press = False
